package com.kongjs.application.controller.rest;

import com.kongjs.application.convert.OperationLogConverter;
import com.kongjs.application.enums.OperationName;
import com.kongjs.application.enums.OperationType;
import com.kongjs.application.model.dto.PageDTO;
import com.kongjs.application.model.dto.PageQueryDTO;
import com.kongjs.application.model.dto.QueryOperationLogDTO;
import com.kongjs.application.model.dto.QueryOperationLogParamDTO;
import com.kongjs.application.model.vo.*;
import com.kongjs.application.service.IOperationLogService;
import com.kongjs.application.util.PageUtil;
import com.kongjs.common.rest.annotation.Rest;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Rest
@RestController
@RequestMapping("/operation/log")
public class OperationLogController {
    @Resource
    private OperationLogConverter operationLogConverter;
    @Resource
    private IOperationLogService operationLogService;

    @PreAuthorize("hasAuthority('api:operationLog:remove:one')")
    @DeleteMapping("/{id}")
    public boolean remove(@PathVariable("id") Long id) {
        return operationLogService.removeById(id);
    }

    @PreAuthorize("hasAuthority('api:operationLog:remove:list')")
    @DeleteMapping
    public boolean remove(@RequestBody List<Long> ids) {
        return operationLogService.removeByIds(ids);
    }

    @PreAuthorize("hasAuthority('api:operationLog:query:page')")
    @GetMapping("/page")
    public PageVO<QueryOperationLogVO> page(@Validated PageQueryVO vo, QueryOperationLogParamVO paramVO) {
        PageQueryDTO dto = new PageQueryDTO();
        dto.setPageNum(vo.getPageNum());
        dto.setPageSize(vo.getPageSize());
        QueryOperationLogParamDTO paramDTO = operationLogConverter.toDto(paramVO);
        PageDTO<QueryOperationLogDTO> pageDTO = operationLogService.page(dto, paramDTO);
        List<QueryOperationLogVO> vos = operationLogConverter.toVos(pageDTO.getItems());
        return PageUtil.pageVo(pageDTO, vos);
    }

    @GetMapping("/types")
    public List<OptionVO> operationTypes() {
        return Arrays.stream(OperationType.values()).map(operationType -> new OptionVO(operationType.getDesc(), String.valueOf(operationType.getValue()))).toList();
    }

    @GetMapping("/names")
    public List<OptionVO> operationNames() {
        return Arrays.stream(OperationName.values()).map(operationName -> new OptionVO(operationName.getDesc(),String.valueOf(operationName.getValue()))).toList();
    }
}
